package com.xls.springboot.controller;

import com.xls.springboot.dao.ProjectRepository;
import com.xls.springboot.dao.StudentRepository;
import com.xls.springboot.entities.Project;
import com.xls.springboot.entities.Student;
import com.xls.springboot.services.ProjectService;
import com.xls.springboot.services.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/projects")
public class ProjectController {
@Autowired
    ProjectService projectService;
@Autowired
    StudentService studentService;
    @Autowired
    ProjectRepository projectRepository;
    @Autowired
    StudentRepository studentRepository;
    @GetMapping
    public String displayProjects(Model model){
        List<Project> projects=projectService.getAll();
        model.addAttribute("project",projects);

        return "projects/list-project";
    }
    @GetMapping("/new")
    public String displayProjectFrom(Model model){
        Project aProject=new Project();
        List<Student> students=studentService.getAll();
        model.addAttribute("project",aProject);
model.addAttribute("allStudents",students);
        return "projects/new-project";
    }
    @PostMapping("/save")
    public String createProject(Model model,@RequestParam List<Long> students, Project project){
        //处理保存数据库的行为
      projectService.save(project);

        //使用重定位防止重复提交
        return "redirect:/projects";
    }
}
